const User = require("../../model/users/index");

class UserService {
  //创建
  async createUser(user_name, password) {
    try {
      const res = await User.create({
        user_name,
        password,
      });
      return res.dataValues;
    } catch (error) {
      return error;
    }
  }
  // 根据任意字段查询
  async getUserInfo(id, user_name, password, is_admin) {
    try {
      const whereOpt = {};
      id && Object.assign(whereOpt, { id });
      user_name && Object.assign(whereOpt, { user_name });
      password && Object.assign(whereOpt, { password });
      is_admin && Object.assign(whereOpt, { is_admin });
      const res = await User.findOne({
        attributes: ["id", "user_name", "password", "is_admin"],
        where: whereOpt,
      });
      return res ? res.dataValues : null;
    } catch (error) {
      return error;
    }
  }
}

module.exports = new UserService();
